package com.jzoffer2;

import java.util.Scanner;

/**
 * TODO
 *
 * @author lefly
 * @version 1.0
 * @classname No58_1
 * @description
 * @date 2021/2/7 下午12:37
 */
public class No58_1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String sentence = sc.nextLine();
        String res = reverseWords(sentence);
        System.out.println(res);

    }

    public static String reverseWords(String s) {
        if(s == null || s.length() == 0 ){
            return "";
        }
        // 去除空格
        s = s.trim();
        char[] str = s.toCharArray();
        int len = str.length;
        int i = 0;
        reverse(str,0,len - 1);
        while (i < len){
            int j = i;
            while (j < len && str[j] != ' '){
                j++;
            }
            reverse(str,i,j - 1);
            // 跳过空格
            while (j < len && str[j] == ' '){
                j++;
            }
            i = j;
        }
        StringBuilder sb = new StringBuilder();
        // 添加
        i = 0;
        while (i < len){
            if(str[i] != ' '){
                sb.append(str[i]);
                i++;
            }else {
                while (i < len && str[i] == ' '){
                    i++;
                }
                sb.append(" ");
            }
        }
        return sb.toString();


    }

    public static void reverse(char[] str,int i,int j){
        while (i < j){
            char temp = str[i];
            str[i] = str[j];
            str[j] = temp;
            i++;
            j--;
        }
    }

}
